<?php

class Webmix_Api_Core extends Core_Api_Abstract
{
  
    const WEBMIX_CONTENT_TYPE_SAMPE = 0;
    const WEBMIX_CONTENT_TYPE_MUSIC = 1;
    
    public function getAdsenseCode(){
        $settingApi = Engine_Api::_()->getApi('settings','core');
        $adsense =  $settingApi->getSetting('webmix.adsense.code',0);
        return $adsense;
    }
    public function getWebmixs($user_id){
        $showsystem = Engine_Api::_()->getApi('settings','core')->getSetting('webmix.showwebmixadmin',0);
        $webmixTable = Engine_Api::_()->getDbtable('webmixes','webmix');
        $select =   $webmixTable->select();
        
        if(empty($showsystem)){
            $haswebmix = $this->hasWebmix($user_id);
            if($haswebmix){
                $select->where('owner_id = ?',$user_id)
                       ->where('type = ?',Webmix_Model_Webmix::WEBMIX_TYPE_USER);
                        
            } 
            else {
                $select->where('type = ?',Webmix_Model_Webmix::WEBMIX_TYPE_SYSTEM);
            }
                
        }
        else {
            $select->where('type = ?',Webmix_Model_Webmix::WEBMIX_TYPE_SYSTEM);
            $sql = "(owner_id = $user_id and type = '".Webmix_Model_Webmix::WEBMIX_TYPE_USER."')";
            $select->orWhere($sql);
        }
        
        $select->order('type')->order('user_id des');
        
        return $webmixTable->fetchAll();
        
                    
    }
    
    public function hasWebmix($user_id){
        $webmixTable = Engine_Api::_()->getDbtable('webmixes','webmix');
        $select = $webmixTable->select()->where('owner_id = ?',$user_id)->where('type = ?',Webmix_Model_Webmix::WEBMIX_TYPE_USER);
        $webmix = $webmixTable->fetchRow($select);
        if($webmix) return true;
        return false;
    }
    
    public function getGoogleRss(){
        $xml = simplexml_load_file('http://news.google.com/?output=rss');
        $items  = $xml->channel->item;
        
        $data = array();
	    foreach($items as $item){
			$data[] = array(
				'title'=>$item->title,
				'link'=>$item->link,
				'time'=> date('H:s',strtotime($item->pubDate))
			);
            
        }
    }
}
